<?php
class Category extends MY_Controller {

	var $currentPage = "category";
	var $caption = "Category";	
	var $action = "";
	
	public function index($start=0,$limit=10) {
		
		$this->load->library('pagination');
		$config['base_url'] = base_url() . 'admin/category/index/';
		$config['total_rows'] = count($this->mCategory->getAllEntries());
		$config['uri_segment'] = 4;
		$config['per_page'] = $limit;
		$config['cur_page'] = $this->uri->segment(4);
		$config['use_page_numbers'] = true;
		$config['num_links'] = 5;
		$config['first_link'] = '<span class="page-far-left">';
		$config['last_link'] = '<span class="page-far-right">';
		
		$this->pagination->initialize($config);
		
		$join_tables = array($this->mTenant);
	    $data['categorylist'] = $this->mCategory->getAllEntriesWithJoin($join_tables,$limit,(($start-1) * $limit));
	    $data['pagination'] = $this->pagination->create_links();
		 
		$this->loadAdminPage($data);
		
	}

	public function create($data='') {
		$this->action = 'add';
		
		$data['tenants'] = $this->mTenant->getAllEntries();
		
		$this->loadAdminPage($data);
	}
	
	public function add() {
		$category = $this->mCategory;
		
		$category->name = trim($this->input->post('name',TRUE));
		$category->code = trim($this->input->post('code',TRUE));
		$category->description = trim($this->input->post('description',TRUE));
		$category->tenant_id = trim($this->input->post('tenant_id',TRUE));
		
		$this->message = INSERT_ERROR;
		
		if(strlen($category->name) != 0 && strlen($category->code) != 0 && strlen($category->tenant_id) != 0) {
			if(count($this->mCategory->find_by_code($category->code))==0) {
				$this->message = INSERT_SUCCESS;
				$category->insert();
			} else {
				$this->message = HAS_DUPLICATE . " name.";
				$data['category'] = $category;
				$this->create($data);
				return;
			}
		} else {
			$data['category'] = $category;
			$this->create($data);
			return;
		}
		
		$this->index();
	}
	
	public function edit($id) {
		$category = $this->mCategory->getEntryById($id);
		
		if(count($category) == 0) {
			show_error("Invalid tenant", 500, "Invalid data");
		}
	
		$data['tenants'] = $this->mTenant->getAllEntries();
		
		$data['category'] = $category[0];
		
		$this->action = 'edit';
		$this->id = $category[0]->id;
		$this->loadAdminPage($data);		
	}
	
	public function update() {
		$category = $this->mCategory;
		
		$category->id = $this->input->post('id',TRUE);
		$category->name = trim($this->input->post('name',TRUE));
		$category->code = trim($this->input->post('code',TRUE));
		$category->description = trim($this->input->post('description',TRUE));
		$category->tenant_id = $this->input->post('tenant_id',TRUE);
		$this->message = UPDATE_ERROR;
		
		if(strlen($category->id) != 0 && strlen($category->tenant_id) != 0 && strlen($category->name) != 0 && strlen($category->code) != 0) {
			
			if(count($this->mCategory->find_by_code_of_others($category->id,$category->code))==0) {
				if($category->update())
					$this->message = UPDATE_SUCCESS;
			} else {
				$this->message = HAS_DUPLICATE . " code.";
			}
		}
		
		$this->edit($category->id);
	}
	
	public function delete($id) {
		$this->message = DELETE_ERROR;
		
		if($this->mCategory->delete($id))
			$this->message = DELETE_SUCCESS;
		
		$this->index();		
	}
}
?>